package org.lib.jpamysql.otm;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import org.lib.jpamysql.otm.dao.OtmBasicDAO;
import org.lib.jpamysql.otm.dao.OtmManyDAO;
import org.lib.jpamysql.otm.dto.AddBasicDTO;
import org.lib.jpamysql.otm.dto.AddHouseDTO;
import org.lib.jpamysql.otm.vo.HouseVO;
import org.lib.jpamysql.otm.vo.OtmBasicVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import lombok.extern.slf4j.Slf4j;

@RestController
@RequestMapping(value="/otmBasic")
@Slf4j
public class OtmBasicController {

	@Autowired
	private OtmBasicDAO basicDao;
	
	@Autowired
	private OtmManyDAO manyDao;
	
	/**
	 * 添加
	 * @author ben
	 * @date 2021-10-18 22:13:29 CST
	 * @param dto
	 * @return
	 */
	@PostMapping(value="/add")
	public Long addBasic(@RequestBody AddBasicDTO dto) {
		String name = dto.getName();
		if (!StringUtils.hasText(name)) {
			return null;
		}
		
		OtmBasic basic = new OtmBasic(name);
		basicDao.save(basic);
		
		return basic.getId();
	}

	@PostMapping(value="/add/house")
	public Long addHouse(@RequestBody AddHouseDTO dto) {
		Long basicId = dto.getBasicId();
		Objects.requireNonNull(basicId);
		
		OtmBasic basic = basicDao.getById(basicId);
		if (basic == null) {
			log.warn("basic不存在: basicId={}", basicId);
			return null;
		}
		
		String house = dto.getHouse();
		if (!StringUtils.hasText(house)) {
			log.warn("house不能为空");
			return null;
		}
		
		OtmMany many = new OtmMany();
		many.setHouse(house);
		many.setBasic(basic);
		manyDao.save(many);
		log.info("new many.id={}", many.getId());
		
		return many.getId();
	}
	
	/**
	 * 查询
	 * @author ben
	 * @date 2021-10-18 22:13:18 CST
	 * @param id
	 * @return
	 */
	@GetMapping(value = "/get")
	public OtmBasicVO get(@RequestParam Long id) {
		// 直接返回会出现错误，会导致循环
		OtmBasic basic = basicDao.getById(id);
		if (basic == null) {
			return null;
		}
		
		OtmBasicVO vo = new OtmBasicVO();
		BeanUtils.copyProperties(basic, vo);
		
		List<HouseVO> houses = new ArrayList<>();
		vo.setHouses(houses);
		List<OtmMany> otmManyList = basic.getManyList();
		if (!CollectionUtils.isEmpty(otmManyList)) {
			otmManyList.forEach(many->{
				HouseVO hvo = new HouseVO();
				BeanUtils.copyProperties(many, hvo);
				houses.add(hvo);
			});
		}
		
		return vo;
	}
	
	/**
	 * 删除
	 * @author ben
	 * @date 2021-10-18 22:13:12 CST
	 * @param id
	 * @return
	 */
	@GetMapping(value = "/del")
	public Boolean del(@RequestParam Long id) {
		if (!basicDao.existsById(id)) {
			log.debug("记录不存在，不做处理");
			return false;
		}
		
		basicDao.deleteById(id);
		log.info("删除 otmBasic 成功: id={}", id);
		return true;
	}
	
}
